package com.encyclopedia.controller;

import com.encyclopedia.entity.User;
import com.encyclopedia.result.JsonRequest;
import com.encyclopedia.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-04-27 16:31
 */
@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest list(User user) {
        List<User> userList = userService.selectlist();
        return JsonRequest.success(userList);
    }


    /**
     * 回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest getById(@PathVariable("id") Integer id) {
        return JsonRequest.success(userService.getById(id));
    }


    /**
     * 根据token获取用户信息
     *
     * @param token token
     * @return 用户信息
     */
    @PostMapping("/token")
    public User getUserByToken(@RequestParam String token) {
        return userService.getUserByToken(token);
    }

    /**
     * 修改/重置密码
     *
     * @param user 用户信息
     * @return JSON
     */
    @PutMapping("/password")
    public Integer updateUserPassword(@RequestBody User user) {
        return userService.updateUserPassword(user);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @PutMapping
    @PreAuthorize("hasAnyAuthority('admin')")
    public JsonRequest update(@RequestBody User user) {
        return JsonRequest.success(userService.updateById(user));
    }


    /**
     * 删除用户
     */
}
